/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hitechbangla.fctu.base.radar;


import com.hitechbangla.fctu.utility.GeneralUtil;
import fctu.simulator.HTB_MAIN;
import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;
import java.util.Random;

/**
 *
 * @author Asus
 */
public class SweepEcm implements EcmDrawable, Serializable {
    
    private int centerXOfArc;
    private int centerYOfArc;
    private int widthOfArc;
    private int heightOfArc;
    private int startAngle;
    private int arcAngle;
    private int aircraftPositionX;
    private int aircraftPositionY;
    private int radarPositionX;
    private int radarPositionY;
    private int radarRange;
    int nextStartAngle;
    int nextArcAngle;
    int nextStartAngle2;
    int nextArcAngle2;
    int nextStartAngle3;
    int nextArcAngle3;
    int sAngle;
    int randomNumber2;
    int randomNumber3;
    int a[][][] ;
    int b[][][];
    int e[][][];
    int d[][][];
    int intervals;
    public SweepEcm(int centerXOfArc, int centerYOfArc, int widthOfArc,
			int heightOfArc, int startAngle, int arcAngle,
			int airPosX, int airPosY) {
	this.centerXOfArc = centerXOfArc;
	this.centerYOfArc = centerYOfArc;
	this.widthOfArc = widthOfArc;
	this.heightOfArc = heightOfArc;
	this.startAngle = startAngle;
	this.arcAngle = arcAngle;
	aircraftPositionX = airPosX;
	aircraftPositionY = airPosY;
	radarRange = (int)(widthOfArc/2);
	radarPositionX = centerXOfArc + radarRange;
	radarPositionY = centerYOfArc + radarRange;
	int start = 4, end = 10;
	int randomNumber;
	Random random = new Random();
	randomNumber = GeneralUtil.getRandomIntegerInRange(start, end, random);
	sAngle = (int)(-startAngle);
	//nextStartAngle = startAngle * randomNumber;
	nextArcAngle = (int) ( (arcAngle * randomNumber)/10  );
	
	randomNumber2 = GeneralUtil.getRandomIntegerInRange(85, 125, random) + startAngle;
	int randomNumber22 = GeneralUtil.getRandomIntegerInRange(1, 5, random);
	//nextArcAngle2 =  randomNumber2 + (startAngle * randomNumber);
	nextArcAngle2 = arcAngle - GeneralUtil.getRandomIntegerInRange(1, 3, random);
	nextArcAngle3 = arcAngle - GeneralUtil.getRandomIntegerInRange(1, 3, random);
	
	randomNumber3 = GeneralUtil.getRandomIntegerInRange(190, 245, random)+startAngle;
	int randomNumber33 = GeneralUtil.getRandomIntegerInRange(1, 5, random);
	//nextArcAngle3 =  randomNumber3 + 90 + (startAngle * randomNumber);
	//nextArcAngle3 = (int) ( ((arcAngle * randomNumber)/10) - randomNumber33  );
	intervals = (int)(radarRange/(int)(2*HTB_MAIN.one_px_equivalent_naut) ) ;
	
	a = new int[intervals][intervals][2];
	b = new int[intervals][intervals][2];
	e = new int[intervals][intervals][2];
	d = new int[intervals][intervals][2];
	
	if(startAngle >0 && startAngle < 90 )
	{
	    int y = radarPositionY + (int) (2 * HTB_MAIN.one_px_equivalent_naut);
	    for (int i = 0; i < intervals; i++) {
		int x = radarPositionX + (int) (2 * HTB_MAIN.one_px_equivalent_naut);
		for (int j = 0; j < intervals; j++) {
//		    a[i][j][0] = radarPositionX+(int)(2*i*HTB_MAIN.one_px_equivalent_naut);
//		    a[i][j][1] = radarPositionY+(int)(2*i*HTB_MAIN.one_px_equivalent_naut);
//		y += (int) (2 * HTB_MAIN.one_px_equivalent_naut) + (int)(i*HTB_MAIN.one_px_equivalent_naut);
		     
		    double distanceBetweenRadAndArcft = GeneralUtil.distance2D(radarPositionX,
			    radarPositionY, x, y);
		    int distance = (int) distanceBetweenRadAndArcft;
		    if((distance+30)< ( radarRange - (int)(HTB_MAIN.one_px_equivalent_naut) ) ) {
			a[i][j][0] = x;
			a[i][j][1] = y;
		    }
		   x += (int) (2 * HTB_MAIN.one_px_equivalent_naut);
		}
		y += (int) (2 * HTB_MAIN.one_px_equivalent_naut);
	    }
	}
	if(startAngle >90 && startAngle < 180 )
	{
	    int y = radarPositionY - (int) (2 * HTB_MAIN.one_px_equivalent_naut);
	    for (int i = 0; i < intervals; i++) {
		int x = radarPositionX + (int) (2 * HTB_MAIN.one_px_equivalent_naut);
		for (int j = 0; j < intervals; j++) {
//		    a[i][j][0] = radarPositionX+(int)(2*i*HTB_MAIN.one_px_equivalent_naut);
//		    a[i][j][1] = radarPositionY+(int)(2*i*HTB_MAIN.one_px_equivalent_naut);
//		y += (int) (2 * HTB_MAIN.one_px_equivalent_naut) + (int)(i*HTB_MAIN.one_px_equivalent_naut);
		     
		    double distanceBetweenRadAndArcft = GeneralUtil.distance2D(radarPositionX,
			    radarPositionY, x, y);
		    int distance = (int) distanceBetweenRadAndArcft;
		    if ( (distance+30)< ( radarRange - (int)(HTB_MAIN.one_px_equivalent_naut) ) ) {
			b[i][j][0] = x;
			b[i][j][1] = y;
		    }
		   x += (int) (2 * HTB_MAIN.one_px_equivalent_naut);
		}
		y -= (int) (2 * HTB_MAIN.one_px_equivalent_naut);
	    }
	}
	if(startAngle >180 && startAngle < 270 )
	{
	    int y = radarPositionY - (int) (2 * HTB_MAIN.one_px_equivalent_naut);
	    for (int i = 0; i < intervals; i++) {
		int x = radarPositionX - (int) (2 * HTB_MAIN.one_px_equivalent_naut)-70;
		for (int j = 0; j < intervals; j++) {
//		    a[i][j][0] = radarPositionX+(int)(2*i*HTB_MAIN.one_px_equivalent_naut);
//		    a[i][j][1] = radarPositionY+(int)(2*i*HTB_MAIN.one_px_equivalent_naut);
//		y += (int) (2 * HTB_MAIN.one_px_equivalent_naut) + (int)(i*HTB_MAIN.one_px_equivalent_naut);
		     
		    double distanceBetweenRadAndArcft = GeneralUtil.distance2D(radarPositionX,
			    radarPositionY, x, y);
		    int distance = (int) distanceBetweenRadAndArcft;
		    if ( distance< ( radarRange - (int)(HTB_MAIN.one_px_equivalent_naut) ) ) {
			e[i][j][0] = x;
			e[i][j][1] = y;
		    }
		   x -= (int) (2 * HTB_MAIN.one_px_equivalent_naut);
		}
		y -= (int) (2 * HTB_MAIN.one_px_equivalent_naut);
	    }
	}
	if(startAngle >270 && startAngle < 360 )
	{
	    int y = radarPositionY + (int) (2 * HTB_MAIN.one_px_equivalent_naut);
	    for (int i = 0; i < intervals; i++) {
		int x = radarPositionX - (int) (2 * HTB_MAIN.one_px_equivalent_naut)-70;
		for (int j = 0; j < intervals; j++) {
//		    a[i][j][0] = radarPositionX+(int)(2*i*HTB_MAIN.one_px_equivalent_naut);
//		    a[i][j][1] = radarPositionY+(int)(2*i*HTB_MAIN.one_px_equivalent_naut);
//		y += (int) (2 * HTB_MAIN.one_px_equivalent_naut) + (int)(i*HTB_MAIN.one_px_equivalent_naut);
		     
		    double distanceBetweenRadAndArcft = GeneralUtil.distance2D(radarPositionX,
			    radarPositionY, x, y);
		    int distance = (int) distanceBetweenRadAndArcft;
		    if ( distance < ( radarRange - (int)(HTB_MAIN.one_px_equivalent_naut) ) ) {
			d[i][j][0] = x;
			d[i][j][1] = y;
		    }
		   x -= (int) (2 * HTB_MAIN.one_px_equivalent_naut);
		}
		y += (int) (2 * HTB_MAIN.one_px_equivalent_naut);
	    }
	}	
    }
    
    @Override
    public void draw(Graphics g, EpmType epmType) {
	//throw new UnsupportedOperationException("Not supported yet.");
	
	//GeneralUtil.log("by ratan rANDOM NUMBER " + randomNumber + " " + startAngle*randomNumber + " "+ centerXOfArc+" "+centerYOfArc+" "+widthOfArc+" "+heightOfArc+" "+startAngle+" "+arcAngle);
	//g.fillArc(centerXOfArc, centerYOfArc, widthOfArc, heightOfArc, startAngle*randomNumber, (int)(arcAngle) );
	//g.fillArc(centerXOfArc, centerYOfArc, widthOfArc, heightOfArc, nextStartAngle, (int) (arcAngle));
	//g.fillArc(centerXOfArc, centerYOfArc, widthOfArc, heightOfArc, randomNumber*(startAngle + 90), arcAngle-10);
	//nextStartAngle += 90;
	//g.fillArc(centerXOfArc, centerYOfArc, widthOfArc, heightOfArc, nextStartAngle, arcAngle - 10);
	GeneralUtil.log("by ratan " + sAngle+" "+startAngle +" "+ centerXOfArc + " " + centerYOfArc + " " + widthOfArc + " " + heightOfArc + " " + startAngle + " " + arcAngle);
//	g.fillArc(centerXOfArc, centerYOfArc, widthOfArc, heightOfArc, nextStartAngle, (int) (arcAngle));
	Color c = g.getColor();
	g.setColor(Color.black);
//	if (epmType != EpmType.FA) {
//	    g.fillArc(centerXOfArc, centerYOfArc, widthOfArc, heightOfArc, startAngle, -nextArcAngle);
//	    g.fillArc(centerXOfArc, centerYOfArc, widthOfArc, heightOfArc, startAngle, nextArcAngle+5);
//	    g.fillArc(centerXOfArc, centerYOfArc, widthOfArc, heightOfArc,randomNumber2, nextArcAngle2);
//	    g.fillArc(centerXOfArc, centerYOfArc, widthOfArc, heightOfArc, randomNumber3, nextArcAngle3);
//	    //g.fillArc(centerXOfArc, centerYOfArc, widthOfArc, heightOfArc, sAngle, nextArcAngle3);
//	    epmType = EpmType.NONE;
//	}
	double distanceBetweenRadAndArcft = GeneralUtil.distance2D(aircraftPositionX, aircraftPositionY
					    , radarPositionX, radarPositionY);
	int distance = (int) distanceBetweenRadAndArcft;
	if(distance>= radarRange)
	{
	    GeneralUtil.log("Aircraft Out Of range!!"+distance+ " : " +radarRange);
	}else{
	    System.out.println("which quadrant : "+startAngle);
	    if( (startAngle>= 0) && (startAngle<=90) )
	    {
		System.out.println("1st qndrnt");
//		g.fillRect(radarPositionX, radarPositionY-3, 6, 6);
//		g.fillRect(radarPositionX+(int)(2*HTB_MAIN.one_px_equivalent_naut), radarPositionY+(int)(3*HTB_MAIN.one_px_equivalent_naut), 6, 6);
//		g.fillRect(radarPositionX+(int)(4*HTB_MAIN.one_px_equivalent_naut), radarPositionY+(int)(3*HTB_MAIN.one_px_equivalent_naut), 6, 6);
//		for(int i=radarPositionX+1; i<=radarRange-1; i++ )
//		{
//		    System.out.println("drawing spot : "+i);
//		    g.fillRect(i, radarPositionY-3, 6, 6);
//		    System.out.println("12st qndrnt");
//		}
		int i=0;
		i = radarPositionX;
		System.out.println("q1");
//		while(i < radarRange )
//		{
//		    int i1 = radarPositionX+(int)(2*HTB_MAIN.one_px_equivalent_naut);
//		    int i2 = radarPositionY+(int)(3*HTB_MAIN.one_px_equivalent_naut);
//		    g.fillRect(i1, i2 , 6, 6);
//		}
//		for(int l=0;l<3;l++)
//		{
//		    int x = a[l][0];
//		    int y = a[l][1];
//		    g.fillRect(x, y, 6, 6);
//		}
		
//		for (int k = 0; k < a.length; k++) {
//		    for (int l = 0; l < a[l].length; l++) {
//			a[k][l][0] = radarPositionX + (int) (2 * i * HTB_MAIN.one_px_equivalent_naut);
//			a[k][l][1] = radarPositionY + (int) (2 * i * HTB_MAIN.one_px_equivalent_naut);
//		    }
//		}
		for (int p = 0; p < intervals; p++) {
		    for (int q = 0; q < intervals; q++) {
			int ix = a[p][q][0];
			int iy = a[p][q][1];
			g.setColor(Color.black);
//			g.fillRect(ix, iy, 6, 6);
			g.drawArc(ix, iy, (int)(80),(int)(100), 90, 20);
		    }
		}
	    }
	    if (startAngle > 90 && startAngle < 180) {
		System.out.println("q2");
		for (int p = 0; p < intervals; p++) {
		    for (int q = 0; q < intervals; q++) {
			int ix = b[p][q][0];
			int iy = b[p][q][1];
			g.setColor(Color.black);
			//g.fillRect(ix, iy, 6, 6);
			g.drawArc(ix, iy, 80, 100, 90, 20);
		    }
		}
	    }
	    if (startAngle > 180 && startAngle < 270) {
		System.out.println("q3");
		for (int p = 0; p < intervals; p++) {
		    for (int q = 0; q < intervals; q++) {
			int ix = e[p][q][0];
			int iy = e[p][q][1];
			g.setColor(Color.black);
			//g.fillRect(ix, iy, 6, 6);
			g.drawArc(ix, iy, 80, 100, 90, 20);
		    }
		}
	    }
	    if (startAngle > 270 && startAngle < 359) {
		System.out.println("q4");
		for (int p = 0; p < intervals; p++) {
		    for (int q = 0; q < intervals; q++) {
			int ix = d[p][q][0];
			int iy = d[p][q][1];
			g.setColor(Color.black);
			//g.fillRect(ix, iy, 6, 6);
			g.drawArc(ix, iy, 80, 100, 90, 20);
		    }
		}
	    }
	}
	
	g.setColor(c);
	//startAngle--;
//	if(startAngle < -360){
//	    startAngle = 0;
//	}
    }	
    
    
}
